package com.example.wendao.view.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.model.entity.News;
import com.example.wendao.model.entity.NewsComment;
import com.example.wendao.model.entity.User;
import com.example.wendao.model.service.UserService;
import com.example.wendao.view.adapter.MessageNewsCommentListAdapter;
import com.example.wendao.viewmodel.CommentViewModel;
import com.example.wendao.viewmodel.NewsViewModel;
import com.example.wendao.viewmodel.UserViewModel;


public class InformationMessageNewsFragment extends Fragment {
    private MessageNewsCommentListAdapter adapter = new MessageNewsCommentListAdapter();
    private UserViewModel userViewModel =null;
    private NewsViewModel newsViewModle = null;
    private String SQL = "";
    private User user;
    private CommentViewModel commentViewModel =null ;
    private int[] arr;
    private RecyclerView recyclerView;
    private LiveData<PagedList<NewsComment>> newsComment;
    private UserService userService;
    private String[] s;

    public InformationMessageNewsFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_information_message_news, container, false);
        recyclerView=view.findViewById(R.id.srl_infomessagenews);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
        user = userViewModel.getCurrentUser();
        //NewsService newsService = new NewsService();
        newsViewModle = new ViewModelProvider(App.getInstance()).get(NewsViewModel.class);
        News[] news = newsViewModle.findAllNewsByAuthorIdInArray(user.getUserId());
        for (int i=0;i<news.length;i++){
            arr[i]= news[i].getNewsId();
        }
        if(arr.length > 0) {
            SQL += " '";
            for(int i=0; i<arr.length-1; i++) {
                SQL = SQL+ arr[i] + "','";
            }
            SQL += arr[arr.length-1] + "'";
        }
        commentViewModel.findAllNewsCommentsBynewsids(SQL).observe(this.getViewLifecycleOwner(), adapter::submitList);
        PageUtils.initRecyclerView(recyclerView, adapter, new LinearLayoutManager(this.getActivity()));
        return view;
    }
}